<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 警告区域 -->
      <el-alert title="注意：只允许为第三级分类设置相关参数" type="warning" show-icon closable> </el-alert>
      <!-- 商品选择器区域 -->
      <span>请选择商品分类：</span>
      <el-cascader v-model="goodsCateArr" :options="goodsCateList" :props="cascaderSelector" @change="handleChange"></el-cascader>
      <!-- 标签页区域 -->
      <el-tabs v-model="activeTabName" @tab-click="handleTabClick">
        <!-- 添加动态参数面板 -->
        <el-tab-pane label="动态参数" name="many">
          <!-- 添加参数按钮 -->
          <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="showAddParamsDialog">添加参数</el-button>
          <!-- 动态参数面板 -->
          <el-table :data="manyParamsList" style="width: 100%">
            <!-- 展开行 -->
            <el-table-column type="expand">
              <template #default="scope">
                <!-- 循环渲染tag标签 -->
                <el-tag v-for="(item, index) in scope.row.attr_vals" :key="index" closable>
                  {{ item }}
                </el-tag>
                <!-- 输入tag -->
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  size="small"
                  ref="saveTagInputRef"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button v-else size="small" @click="showInput(scope.row)" class="input-new-tag">new tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <!-- 参数名称 -->
            <el-table-column prop="attr_name" label="参数名称"> </el-table-column>
            <!-- 操作 -->
            <el-table-column label="操作">
              <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
              <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <!-- 添加静态属性面板 -->
        <el-tab-pane label="静态属性" name="only">
          <!-- 添加属性按钮 -->
          <el-button type="primary" size="mini" :disabled="isBtnDisabled" @click="showAddParamsDialog">添加属性</el-button>
          <!-- 静态属性面板 -->
          <el-table :data="onlyParamsList" style="width: 100%">
            <template #default="">
              <el-table-column type="index"> </el-table-column>
              <!-- 参数名称 -->
              <el-table-column prop="attr_name" label="属性名称"> </el-table-column>
              <!-- 操作 -->
              <el-table-column label="操作">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
              </el-table-column>
            </template>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加参数对话框 -->
    <el-dialog :title="'添加' + titleText" :visible.sync="addParamsVisible" width="50%" @close="clearForm">
      <!-- 添加参数表单 -->
      <el-form :model="addParamsForm" :rules="addParamsFormRules" ref="addParamsFormRef" label-width="100px">
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="addParamsForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <!-- 页脚按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParamsVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddParams">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created () {
    this.getGoodsCateList()
  },
  data () {
    return {
      /* 商品分类选择器中表示所选择的商品数组 */
      goodsCateArr: [],
      /* 商品分类列表 */
      goodsCateList: [],
      /* 级联选择器对象 */
      cascaderSelector: {
        expandTrigger: 'hover',
        label: 'cat_name',
        value: 'cat_id',
        children: 'children'
      },
      /* 标签页选择时的名字 */
      activeTabName: 'many',
      /* 静态参数列表 */
      onlyParamsList: [],
      /* 动态参数列表 */
      manyParamsList: [],
      /* 控制添加参数对话框是否显示 */
      addParamsVisible: false,
      /* 添加参数对象 */
      addParamsForm: {
        attr_name: ''
      },
      /* 添加参数对象规则 */
      addParamsFormRules: {
        attr_name: [{ required: true }]
      },
      /* 是否显示标签 */
      inputVisible: false,
      /* 标签名 */
      inputValue: ''
    }
  },
  methods: {
    /* 获取分类列表 */
    async getGoodsCateList () {
      const { data: res } = await this.$http.get('categories', {
        params: {
          type: 3
        }
      })
      if (res.meta.status !== 200) {
        this.$message.error('获取分类列表失败')
      }
      this.goodsCateList = res.data
    },
    /* 获取参数列表 */
    async getParamsList () {
      const { data: res } = await this.$http.get(`categories/${this.goodsCateArr[2]}/attributes`, {
        params: {
          sel: this.activeTabName
        }
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取参数列表失败')
      }
      // 将获取到的响应数据中的attr_vals中的标签字符串变成数组
      res.data.forEach(el => {
        el.attr_vals = el.attr_vals ? el.attr_vals.split(' ') : []
        // 添加属于自己的inputVisible 好inputValue
        el.inputVisible = false
        el.inputValue = ''
      })
      if (this.activeTabName === 'only') {
        this.onlyParamsList = res.data
      } else if (this.activeTabName === 'many') {
        this.manyParamsList = res.data
      }
    },
    /* 监听级联选择器选择时的事件 */
    handleChange () {
      this.getParamsList()
    },
    /* 监听标签页被点击时的事件 */
    handleTabClick () {
      this.getParamsList()
    },
    /* 监听加参数和添加属性按钮点击事件 */
    showAddParamsDialog () {
      this.addParamsVisible = true
    },
    /* 监听提交表弟事件 */
    AddParams () {
      this.$refs.addParamsFormRef.validate(async valid => {
        if (!valid) {
          return false
        }
        const { data: res } = await this.$http.post(`categories/${this.goodsCateArr[2]}/attributes`, {
          attr_name: this.addParamsForm.attr_name,
          attr_sel: this.activeTabName
        })
        if (res.meta.status !== 201) {
          this.$http.error('添加失败')
        }
        this.$message.success('添加成功')
        this.addParamsVisible = false
        this.getParamsList()
      })
    },
    /* 监听对话框关闭事件 */
    clearForm () {
      this.$refs.addParamsFormRef.resetFields()
    },
    /* 监听标签被点击事件 */
    showInput (row) {
      row.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInputRef.$refs.input.focus()
      })
    },
    /* 监听标签修改后事件 */
    handleInputConfirm (row) {
      if (!row.inputValue.trim()) {
        row.inputValue = ''
        row.inputVisible = false
        return false
      }
      row.attr_vals.push(row.inputValue.trim())
      row.inputValue = ''
      row.inputVisible = false
    }
  },
  computed: {
    /* 按钮是否被禁用 */
    isBtnDisabled () {
      if (this.goodsCateArr.length === 3) {
        return false
      }
      return true
    },
    /* 添加动态参数和添加静态属性对话框标题 */
    titleText () {
      if (this.activeTabName === 'many') {
        return '动态参数'
      } else {
        return '静态属性'
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-breadcrumb {
  font-size: 12px;
  margin-bottom: 20px;
}
.el-card {
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15) !important;
}
.el-alert {
  margin-bottom: 20px;
}
.el-tag {
  margin-right: 10px;
}
.input-new-tag {
  width: 120px;
}
</style>
